import com.gargoylesoftware.htmlunit.html.DomAttr
import com.gargoylesoftware.htmlunit.html.DomElement

/**
 *
 * @author siuming
 *
 */

/**
 *
 * @param page
 * @param xpath
 * @return
 */
def __eval_xpath(page, xpath) {
    page.getByXPath(xpath)
}

/**
 *
 * @param page
 * @param xpath
 * @return
 */
def __eval_text(page, xpath) {
    ele = page.getFirstByXPath(xpath);
    if (null == ele) {
        return "";
    }

    if (ele instanceof DomAttr) {
        return ele.value;
    }

    if (ele instanceof DomElement) {
        return ele.asText();
    }

    ele.toString()
}

/**
 *
 * @param page
 * @param xpath
 * @return
 */
def __eval_textlist(page, xpath) {
    eles = page.getByXPath(xpath);
    eles.collect {
        if (it instanceof DomAttr) {
            return it.value;
        }

        if (it instanceof DomElement) {
            return it.asText();
        }

        it.toString()
    }
}